package com.example.a14110100420.yuejian.WAdapter.personal;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WSerivice.PersonalService;
import com.example.a14110100420.yuejian.Wbase.Conast;
import com.example.a14110100420.yuejian.Wbean.personal.MyConcern;
import com.example.a14110100420.yuejian.Wviewholder.personal.MyGuanzhuViewholder;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wanger on 2017/10/23.
 */

public class MyGuanzhuListAdapter extends RecyclerView.Adapter {
    public LayoutInflater layoutInflater;
    public Context context;
    public List list;
    private String clubid;



    public MyGuanzhuListAdapter(Context context,List list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = layoutInflater.inflate(R.layout.myguanzhulist_adapter,parent,false);
        MyGuanzhuViewholder viewholder = new MyGuanzhuViewholder(itemView);
        return viewholder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        MyGuanzhuViewholder viewholder = (MyGuanzhuViewholder) holder;
        final MyConcern concern = (MyConcern) list.get(position);
        final String clubid = concern.getFan_id();
        final String userid = concern.getUserid();
        viewholder.focus_name.setText(concern.getName());
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/"+concern.getThumb())
                .into(viewholder.focus_touxiang);
       viewholder.focus_guanzhu.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Retrofit retrofit = new Retrofit.Builder()
                       .baseUrl(Conast.base_Url)
                       .addConverterFactory(GsonConverterFactory.create())
                       .build();
               PersonalService service = retrofit.create(PersonalService.class);
               Call<Integer> call = service.concelFocusClub(clubid,userid);
               call.enqueue(new Callback<Integer>() {
                   @Override
                   public void onResponse(Call<Integer> call, Response<Integer> response) {
                       if (response.isSuccessful()){
                           Toast.makeText(context, "取消关注成功",Toast.LENGTH_SHORT).show();

                       }else {
                           Toast.makeText(context, "取消失败",Toast.LENGTH_SHORT).show();
                       }
                   }

                   @Override
                   public void onFailure(Call<Integer> call, Throwable t) {

                   }
               });

           }
       });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    private void concelFocus(){

    }

}
